<?php
namespace app\admin\model;

use think\Model;
class TermModel extends Model
{
    public function getList($field=[]){
        if(!empty($field))
            $this->field($field);
        $list = $this->select();
        return $list;
    }
	
	public function addModel()
	{	
        $add = [
            'name'          => I('post.name'),
            'term_tpl'      => I('post.term_tpl'),
            'term_tags'     => json_encode([]),
            'content_tpl'   => I('post.content_tpl'),
            'content_tags'  => json_encode([]),
            'create_time'   => time(),
        ];
		return $this->add($add);
	}

	public function addTag($id, $type)
	{
        //id为空则不进行任何操作
        if(empty($id))
            return false;
        //检测参数是否合法
        if(!$field = $this->getTagField($type))
            return false;
        $tag = I('post.tag');
        $tagName = I('post.tag_name');
        $tagType = I('post.tag_type');
        //检测参数是否有空
        if(empty($tag)||empty($tagName)||empty($tagType))
            return false;
        $where = [
            'id'=>$id
        ];
        $this->where($where)->field($field);
        $tagList = $this->find();
        $tagList = json_decode($tagList[$field], true);
        //检查是否有冲突
        if(!empty($tagList[$tag]))
            return false;
        $tagList = array_merge($tagList,
            [$tag=>['tag_name'=>$tagName, 'tag_type'=>$tagType]]);
        $tagList = json_encode($tagList);
        $save[$field] = $tagList;
        $this->where($where);
		return $this->save($save);
	}

    public function delTag($id, $tag, $type)
    {
        //id为空则不进行任何操作
        if(empty($id)|| $tag===null)
            return false;
        if(!$field = $this->getTagField($type))
            return false;
        $where = [
            'id'=>$id
        ];
        $this->where($where)->field($field);
        $tagList = $this->find();
        $tagList = json_decode($tagList[$field], true);
        if(empty($tagList[$tag]))
            return false;
        unset($tagList[$tag]);
        $tagList = json_encode($tagList);
        $save[$field] = $tagList;
        $this->where($where);
		return $this->save($save);
    }

	public function setModel($id)
	{	
        if(empty($id))
            return false;
        $where = ['id' => $id];
        $add = [
            'name'          => I('post.name'),
            'term_tpl'      => I('post.term_tpl'),
            'content_tpl'   => I('post.content_tpl'),
        ];
		return $this->where($where)->save($add);
	}

    public function getIdModelInfo($id)
    {
        if(empty($id))
            return false;
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->find();
    }

    /**
     * 获取经过处理的自定义标签字段列表
     */
    public function getIdModelTags($id, $type)
    {
        if(empty($id))
            return false;
        if(!$field = $this->getTagField($type))
            return false;
        $where = [
            'id' => $id,
        ];
        $result = $this->where($where)->field($field)->find();
        $re = json_decode($result[$field], true);
        return $re;
    }

    public function delModel($id)
    {
        if(empty($id))
            return false;
        return $this->delete($id);
    }

    private function getTagField($type)
    {
        //检测参数是否合法
        if($type != 'term' && $type != 'content')
            return false;
        return $field = $type.'_tags';
    }
}

